//
//  HGDB.m
//  中关村在线
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "HGDB.h"

@implementation HGDB

//创建单例对象
+ (HGDB *)sharedHGDB{
    static HGDB *hg = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        hg = [[HGDB alloc] init];
    });
    
    return hg;
}

//打开数据库

- (void)openHGDB{
    
    //获取沙盒路经
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    //拼接数据库路径
    
    NSString *path = [document stringByAppendingPathComponent:@"zhongguanchun.sqlite"];
    NSLog(@"%@", path);
    int result = sqlite3_open(path.UTF8String, &dbPoint);
    
    
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }else{
        NSLog(@"打开失败");
    }
    
}

- (void)createTable{
    
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists News (number integer primary key autoincrement , stitle text, imgsrc text, url text)"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表格成功");
    }else{
        NSLog(@"创建表单失败");
    }
    
    
}

//插入数据

- (void)insertData:(News *)model{
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into News (stitle, imgsrc, url) values('%@', '%@', '%@')", model.stitle, model.imgsrc, model.url];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入数据成功");
    }else{
        NSLog(@"插入数据失败");
    }
    
    
    
}

//删除数据

- (void)deleteData:(News *)model{
    
    NSString *sqlStr = [NSString stringWithFormat:@"delete from News where stitle = '%@'", model.stitle];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    
    
}


//修改数据

- (void)updateData:(News *)model{
    
    NSString *sqlStr = [NSString stringWithFormat:@"update News set stitle = '%@', imgsrc = '%@'  where url = '%@'", model.stitle, model.imgsrc, model.url];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    }else{
        NSLog(@"更新失败");
    }
}


//查询数据

- (NSMutableArray *)selectData{
    
    
    NSString *sqlStr = [NSString stringWithFormat:@"select * from News order by number desc"];
    
    sqlite3_stmt *stmt  = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            News *model = [News new];
            
            const unsigned char *stitle = sqlite3_column_text(stmt, 1);
            const unsigned char *imgsrc = sqlite3_column_text(stmt, 2);
            const unsigned char *url = sqlite3_column_text(stmt, 3);
            
            model.stitle = [NSString stringWithUTF8String:(const char *)stitle];
            model.imgsrc = [NSString stringWithUTF8String:(const char *)imgsrc];
            model.url = [NSString stringWithUTF8String:(const char *)url];
            
            
            [arr addObject:model];
            
            
        }
        
        
        
    }
    
    
    
    return arr;
    
}


- (NSMutableArray *)selectDataAll{
    
    
    NSString *sqlStr = [NSString stringWithFormat:@"select * from News"];
    
    sqlite3_stmt *stmt  = nil;
    
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    NSMutableArray *arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            News *model = [News new];
            
            const unsigned char *stitle = sqlite3_column_text(stmt, 1);
            const unsigned char *imgsrc = sqlite3_column_text(stmt, 2);
            const unsigned char *url = sqlite3_column_text(stmt, 3);
            
            model.stitle = [NSString stringWithUTF8String:(const char *)stitle];
            model.imgsrc = [NSString stringWithUTF8String:(const char *)imgsrc];
            model.url = [NSString stringWithUTF8String:(const char *)url];
            
            
            [arr addObject:model];
            
            
        }
        
        
        
    }
    
    
    
    return arr;
    
    
}



@end
